package com.ming.reggie.service.impl;

import com.ming.reggie.dto.DishDto;
import com.ming.reggie.entity.Dish;
import com.ming.reggie.entity.DishFlavor;
import com.ming.reggie.mapper.DishFlavorMapper;
import com.ming.reggie.mapper.DishMapper;
import com.ming.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    /**
     * 添加菜品与菜品口味
     *
     * @param dishDto
     * @return
     */
    @Override
    public boolean addDish(DishDto dishDto) {
        //完善信息
        dishDto.setStatus(1);
        dishDto.setSort(0);
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateTime(LocalDateTime.now());
        //调用数据访问层
        dishMapper.addDish(dishDto);

        List<DishFlavor> flavorList = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
        }

        dishFlavorMapper.addDishFlavor(flavorList);

        redisTemplate.delete("dish_*");

        return true;
    }

    /**
     * 查询对应类别下菜品列表显示
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findBycategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.findByCategoryId(categoryId);
        return dishList;
    }

    /**
     * 根据类别和状态查询菜品列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<DishDto> findByCategoryId(Long categoryId, Integer status) {

        //查询缓存类别的菜品列表
        String key = "dish_"+categoryId+"_"+status;
        List<DishDto> dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);
        //查询到的数据为null时,调用数据库查询
        if(dishDtoList== null) {

            List<Dish> dishList = dishMapper.findByCategoryIdAndStatus(categoryId, status);
                dishDtoList = dishList.stream().map((dish -> {
                //实例dishDto 封装对象
                DishDto dishDto = new DishDto();

                //将dish对象赋值给dishdto
                BeanUtils.copyProperties(dish, dishDto);
                List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dishDto.getId());
                dishDto.setFlavors(dishFlavorList);
                return dishDto;
            })).collect(Collectors.toList());

                //将数据库查到的数据写入缓存
            redisTemplate.opsForValue().set(key,dishDtoList);

        }

        return dishDtoList;
    }
}
